// echo partial file, should not compile along

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
using HarmonyLib;
using Utils;
namespace GongfaEffect;

using GameData.Domains;

public partial class GongfaEffect {
//! 这是一个专门针对太吾的Mod，这个Mod允许太吾在一定程度上操纵NPC的过月行为
    public static class LuanQingZhang {
        public static sbyte DefKey_MakeLove=typeof(Config.PersonalNeed.DefKey).GetField("MakeLove",(BindingFlags)(-1))==null?Config.PersonalNeed.DefKey.MakeLove:(sbyte)typeof(Config.PersonalNeed.DefKey).GetField("MakeLove",(BindingFlags)(-1)).GetValue(null);
        public static ushort RelationType_Adored=typeof(GameData.Domains.Character.Relation.RelationType).GetField("Adored",(BindingFlags)(-1))==null?GameData.Domains.Character.Relation.RelationType.Adored:(ushort)typeof(GameData.Domains.Character.Relation.RelationType).GetField("Adored",(BindingFlags)(-1)).GetValue(null);
        public const short Defkey_LuanQingZhang=289; // 目前这个方法并没有被暴露在defkey之外，因此只能如此使用。

        public static Dictionary<int,SuddenLove> EffectDict=new Dictionary<int,SuddenLove>();
        public static SuddenLove GetOrDefault(int CharacterId){
            SuddenLove effect;
            if(!EffectDict.TryGetValue(CharacterId, out effect)){
                effect=new SuddenLove(CharacterId);
                EffectDict[CharacterId]=effect;
            }
            return effect;
        }

        public static void Patch(RobustHarmonyInstance HarmonyInstance, string ModIdStr){
            HarmonyInstance.PatchAll(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType);
        }

        [HarmonyPostfix]
        [HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.Wuxianjiao.DefenseAndAssist.LuanQingZhang),"OnEnable")]
        public static unsafe void OnEnable(GameData.Domains.SpecialEffect.CombatSkill.Wuxianjiao.DefenseAndAssist.LuanQingZhang __instance, GameData.Common.DataContext context) {
            GameData.DomainEvents.Events.RegisterHandler_NormalAttackBegin(new GameData.DomainEvents.Events.OnNormalAttackBegin(GetOrDefault(__instance.CharacterId).OnNormalAttackBegin));
            //GameData.Utilities.AdaptableLog.Info("PeiRanJue.OnEnable-执行完毕,charid="+__instance.CharacterId);
        }
        static MethodInfo TryGetElement_PregnantStates=typeof(GameData.Domains.Character.CharacterDomain).GetMethod("TryGetElement_PregnantStates",(BindingFlags)(-1));
        [HarmonyPostfix]
        [HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.Wuxianjiao.DefenseAndAssist.LuanQingZhang),"OnDisable")]
        public static unsafe void OnDisable(GameData.Domains.SpecialEffect.CombatSkill.Wuxianjiao.DefenseAndAssist.LuanQingZhang __instance, GameData.Common.DataContext context, bool ___IsDirect) {
            GameData.DomainEvents.Events.UnRegisterHandler_NormalAttackBegin(new GameData.DomainEvents.Events.OnNormalAttackBegin(GetOrDefault(__instance.CharacterId).OnNormalAttackBegin));
            EffectDict.Remove(__instance.CharacterId);
            if(!___IsDirect){
                object[] param=new object[]{__instance.CharacterId, null};
                if((bool)TryGetElement_PregnantStates.Invoke(DomainManager.Character,param)){
                    var pregnantState=(GameData.Domains.Character.PregnantState)param[1];
                    pregnantState.FatherFeatureIds=new List<short>(pregnantState.MotherFeatureIds);
                    pregnantState.CreateFatherRelation=false;
                    pregnantState.FatherId = __instance.CharacterId;
                    pregnantState.FatherGenome = __instance.CharObj.GetGenome();
                }
            }
            int garbage;
            if(DomainManager.Character.TryGetElement_PregnancyLockEndDates(__instance.CharacterId, out garbage)){
                DomainManager.Character.RemovePregnantLock(context, __instance.CharacterId);
            }
            //GameData.Utilities.AdaptableLog.Info("PeiRanJue.OnDisable-执行完毕,charid="+__instance.CharacterId);
        }
        public class SuddenLove {
            int CharacterId;
            public static short DirEff_CanHuTi=1583;
            public SuddenLove(int CharacterId){
                this.CharacterId=CharacterId;
            }
            public void OnNormalAttackBegin(GameData.Common.DataContext context, GameData.Domains.Combat.CombatCharacter attacker, GameData.Domains.Combat.CombatCharacter defender, sbyte trickType, int pursueIndex){
                // bool flag = !this.IsSrcSkillPerformed || attacker != base.CombatChar || pursueIndex > 0 || !/*DomainManager.Combat.InAttackRange(base.CombatChar)*/;
                if(attacker.GetId() == this.CharacterId) {
                    GameData.Domains.Character.Ai.PersonalNeed need = GameData.Domains.Character.Ai.PersonalNeed.CreatePersonalNeed(DefKey_MakeLove, this.CharacterId);
                    // GameData.Utilities.AdaptableLog.Info("pre- "+defender.GetId()+" love "+attacker.GetId()+", DefKey_MakeLove="+DefKey_MakeLove+", currNeedCount="+defender.GetCharacter().GetPersonalNeeds().Count);
                    // GameData.Utilities.AdaptableLog.Info(string.Format("{0}({1})-{2}", Config.PersonalNeed.Instance[need.TemplateId].Name, need.RemainingMonths, need.Amount));
                    var victim=defender.GetCharacter();
                    var pnl=victim.GetPersonalNeeds();
                    pnl.Add(need);
                    victim.SetPersonalNeeds(pnl,context);
                    DomainManager.Combat.ShowSpecialEffectTips(context, this.CharacterId, DirEff_CanHuTi, 0);
                    // GameData.Utilities.AdaptableLog.Info("post "+defender.GetId()+" love "+attacker.GetId()+", DefKey_MakeLove="+DefKey_MakeLove+", currNeedCount="+defender.GetCharacter().GetPersonalNeeds().Count);
                }
            }
        }
    }
}
